using System.Collections;
using UnityEngine;
/// <summary>
/// 定时自动销毁
/// </summary>
public class Autodestruction : MonoBehaviour
{
    [Tooltip("是否销毁，如果不销毁就是失活")]
    [SerializeField] private bool destoryGameobjectl;
    [Tooltip("生存时间")]
    [SerializeField] private float lifettime = 3f;
    private WaitForSeconds waitLifetime;
    private void Awake()
    {
        waitLifetime = new WaitForSeconds(lifettime);//创建一个等待3秒的等待对象
    }
   
    void OnEnable ()
    {
        StartCoroutine(DeactiveCoroutine());
    }
    /// <summary>
    /// 等待waitLifetime，要么销毁，要么设置false
    /// </summary>
    /// <returns></returns>
    IEnumerator DeactiveCoroutine()
    {
        yield return waitLifetime;
        if (destoryGameobjectl)
        {
            Destroy(gameObject);
        }
        else
        {
            gameObject.SetActive(false);
        }
    }
}
